package codeRandomThoughts.Test404左叶子之和;

import codeRandomThoughts.utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    //首先想到的是层序遍历,对于每一个结点,都判断他有没有左叶子,有的话加到sum里面
    public int sumOfLeftLeaves(TreeNode root) {
        int sum = 0;
        TreeNode node = root;
        if (node == null || node.left == null && node.right == null) {
            return sum;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);

        while (!queue.isEmpty()) {
            int leftNum = 0;
            node = queue.poll();
            sum+=leftNum(node);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        return sum;
    }
     int leftNum(TreeNode node){
        if (node.left!=null && (node.left.left==null && node.left.right==null)){
            return node.left.val;
        }
        return 0;
     }

}
